package com.atguigu.cms.controller;

import com.atguigu.cms.entity.CrmBanner;
import com.atguigu.cms.service.CrmBannerService;
import com.atguigu.utils.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2020-07-11
 */
@RestController
@RequestMapping("/cms/crmBannerAdmin")
@Api(description = "Banner后台管理员接口")
@CrossOrigin
public class CrmBannerAdminController {
    @Autowired
    CrmBannerService crmBannerService;

    @ApiOperation(value = "分页获取banner数据")
    @PostMapping("/getBanner/{current}/{limit}")
    public R getBanner(@PathVariable long current, @PathVariable long limit, @RequestBody(required = false) CrmBanner crmBanner) {
        if (crmBanner != null) {

        }
        Page<CrmBanner> page = new Page<>(current, limit);
        IPage<CrmBanner> iPage = crmBannerService.page(page, null);
        return R.ok().data("bannerItems", iPage.getRecords()).data("bannerTotal", iPage.getTotal());
    }

    @ApiOperation(value = "添加banner数据")
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {

        try {
            boolean b = crmBannerService.save(crmBanner);
            if (b) {
                return R.ok();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();

    }

    @ApiOperation(value = "删除banner数据")
    @DeleteMapping("/delBanner/{id}")
    public R delBanner(@PathVariable String id) {
        try {
            boolean b = crmBannerService.removeById(id);
            if (b) {
                return R.ok();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }

    @ApiOperation(value = "修改banner数据")
    @PutMapping("/updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner) {
        try {
            boolean b = crmBannerService.updateById(crmBanner);
            if (b) {
                return R.ok();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }
}

